package com.puzzlebazar.client.util;

import com.google.gwt.user.client.ui.TextBox;

public class ValidatedField extends LabeledFieldImpl {

	/**
	 * Creates a labeled field which minimal length is validated  and a specific text box
	 * 
	 * @param textBox The textbox to use
	 * @param labelText The text to put in the label
	 * @param minLength The minimum valid length
	 * @param maxLength The maximum valid length
	 * @param filter The filter to use on this input (can be null for no filtering)
	 * @param errorMessage The error message to display when the minimum length is not reached
	 */
	protected ValidatedField( TextBox textBox, String labelText, 
			final int minLength, final int maxLength, BasicFilter filter,
			final String errorMessage ) {
		super( textBox, labelText, maxLength, filter );
		
		initialize(minLength, errorMessage);		
	}

	/**
	 * Creates a labeled field which minimal length is validated
	 * 
	 * @param labelText The text to put in the label
	 * @param minLength The minimum valid length
	 * @param maxLength The maximum valid length
	 * @param filter The filter to use on this input (can be null for no filtering)
	 * @param errorMessage The error message to display when the minimum length is not reached
	 */
	public ValidatedField( String labelText, 
			int minLength, int maxLength, BasicFilter filter,
			String errorMessage ) {
		super( labelText, maxLength, filter );
		
		initialize(minLength, errorMessage);				
		
	}
	

	/**
	 * Completes the constructor initialization
	 * 
	 * @param minLength The minimum valid length
	 * @param errorMessage The error message to display when the minimum length is not reached
	 */
	private void initialize(final int minLength, final String errorMessage) {
		addValidationCallback( new ValidationCallback() {
			@Override
			public String validate(String text) {
				if( text.length() < minLength ) return errorMessage;
				return null;
			}
		});
	}	
	

}
